const router = require('koa-router')();
const index = require('../app/model/mongodb/index');
const feeds = require('../app/model/feeds.mongo');

router.prefix('/api/v1/feeds/account');

router.get('/', function (ctx, next) {
  ctx.body = 'this is a feedsAccountManagement response!'
});

/***********************
 *  GET Amz Seller account
 ************************/
router.get('/fetch', async (ctx, next) => {
  let sellerId = ctx.headers.sellerid;
  let mwsAuthToken = ctx.headers.mwsauthtoken;

  let idType = ctx.params.IDType;
  let id = ctx.params.ID;
  //idType = "ptAccount_storeId";
  //id = '5f7b25937f864822cca8d6ee';

  //Use the SellerId to get other fields from amzSellerAccount table.
  try {
    //If IDType = “ptAccount_storeId”, use the ID to get sellerId from ptSellerAccount table
    if (idType == "ptAccount_storeId") {
      sellerId = feeds.findSellIdByStoreId(id);
    }
    let json = await feeds.findAmzSellerAccountBySellId(sellerId);
    
    ctx.response.status = 200;
    ctx.response.body = {
      data: json,
    };
  } catch (error) {
    ctx.response.status = 500;
    ctx.response.body = {
      data: error,
    };
  }
});

module.exports = router;